﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FreeProperty.Views.Shared;
using FreeProperty.Data;
using System.Data.SqlTypes;

namespace FreeProperty.Views.Main
{
    public partial class PostProperty : SiteBasePageWithSession
    {
        public delegate void SimpleDelegate();
        string propertyType;
        string usageType;
        string editId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                propertyType = Request.QueryString["ptype"];
                usageType = Request.QueryString["utype"];
                editId = Request.QueryString["editid"];
                SetUI();
            }
            else
            {
                switch (hdnMode.Value.ToUpper())
                {
                    case "POSTPROPERTY":
                        PropertyDesc oPropertyDesc = GetPropertyFromUI();
                        oPropertyDesc.Save();
                        lblError.Text = "Property posted successfully.";
                        break;
                    
                    case "CHANGESTATE":
                        BindDistrictData();
                        break;
                }
            }
        }

        private PropertyDesc GetPropertyFromUI()
        {
            PropertyDesc oPropertyDesc = new PropertyDesc();
            if (!string.IsNullOrEmpty(hdnPropertyId.Value))
            {
                oPropertyDesc.ID = new SqlGuid(hdnPropertyId.Value);
            }
            oPropertyDesc.PropertyType = (FreePropertyType)Convert.ToInt32(ddlPropertyType.SelectedValue);
            oPropertyDesc.PurposeType = (FreePropertyUsageType)Convert.ToInt32(ddlPurpose.SelectedValue);
            oPropertyDesc.DistrictID = Convert.ToInt32(ddlDistrict.SelectedValue);

            oPropertyDesc.Location = tbLocation.Value.Trim();
            oPropertyDesc.Address = tbAddress.Text.Trim();
            if (tbprice.Text.Trim() == "")
            {
                oPropertyDesc.Price = SqlInt32.Null;
            }
            else
            {
                oPropertyDesc.Price =   Convert.ToInt32(tbprice.Text.Trim());
            }
            oPropertyDesc.PropertyDescription = tbPropertyDetails.Text.Trim();

            oPropertyDesc.ContactNo = tbPhoneNo.Value.Trim();
            oPropertyDesc.Name = tbName.Value.Trim();
            oPropertyDesc.PreferredTime = tbPreferredTime.Value.Trim();
            oPropertyDesc.EmailId = tbEmailId.Value.Trim();

            oPropertyDesc.UserID = ActiveUser.ID;
            return oPropertyDesc;
        }
        
        private void SetUI()
        {
            FreePropertyUtil.BindNameValue(ddlPropertyType, FreePropertyConstants.CACHE_PROPERTY_TYPE);
            FreePropertyUtil.BindNameValue(ddlState, FreePropertyConstants.CACHE_ALL_STATES);
            FreePropertyUtil.BindNameValue(ddlPurpose, FreePropertyConstants.CACHE_USAGE_TYPES);
            ddlState.SelectedIndex = 0;
            BindDistrictData();
            if (!String.IsNullOrEmpty(propertyType))
            {
                ddlPropertyType.SelectedValue = propertyType;
            }
            if (!String.IsNullOrEmpty(usageType))
            {
                ddlPurpose.SelectedValue = usageType;
            }
            hdnContactName.Value = ActiveUser.Name;
            hdnContactEmail.Value = ActiveUser.EmailID;
            hdnContactPhone.Value = ActiveUser.ContactNo;
            if (!string.IsNullOrEmpty(editId))
            {
                LoadProperty(editId);
            }
        }

        private void LoadProperty(string editId)
        {
            PropertyDesc oPropertyDesc = new PropertyDesc();
            oPropertyDesc.ID = new SqlGuid(editId);
            oPropertyDesc.LoadProperty();
            hdnPropertyId.Value = editId;
            ddlPropertyType.SelectedValue = Convert.ToInt32(oPropertyDesc.PropertyType).ToString();
            ddlPurpose.SelectedValue = Convert.ToInt32(oPropertyDesc.PurposeType).ToString();
            ddlState.SelectedValue = oPropertyDesc.StateID.ToString();
            BindDistrictData();
            ddlDistrict.SelectedValue = oPropertyDesc.DistrictID.ToString();
            if (oPropertyDesc.Price.IsNull)
            {
                chkPriceUnknown.Checked = true;
            }
            else
            {
                tbprice.Text = oPropertyDesc.Price.ToString();
            }
            tbLocation.Value = oPropertyDesc.Location;
            tbAddress.Text = oPropertyDesc.Address;
            tbPropertyDetails.Text = oPropertyDesc.PropertyDescription;
            tbName.Value = oPropertyDesc.Name;
            tbPhoneNo.Value = oPropertyDesc.ContactNo;
            tbEmailId.Value = oPropertyDesc.EmailId;
            tbPreferredTime.Value = oPropertyDesc.PreferredTime;
        }
        private void BindDistrictData()
        {
            District oDistrict = new District();
            FreePropertyUtil.BindDropdown(ddlDistrict, oDistrict.LoadAllNameValue(Convert.ToInt32(ddlState.SelectedValue)), false);

        }

    }
}
        